using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Media;
using Sanford.Multimedia.Midi;
using System.IO;
using AxShockwaveFlashObjects;
using System.Runtime.InteropServices;

namespace ComfySandBox
{
    public partial class frmMain : Form
    {

        [DllImport("winmm.dll")]
        private static extern long mciSendString(string strCommand,StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
         
        private const char LEFT_ONE = 'j';
        private const char LEFT_TWO = 'h';
        private const char LEFT_THREE = 'g';
        private const char LEFT_FOUR = 'f';
        private const char LEFT_FIVE = 'd';

        private const char CENTER_TOP = 'e';
        private const char CENTER_LEFT = 'y';
        private const char CENTER_CENTER = 'u';
        private const char CENTER_RIGHT = 'r';
        private const char CENTER_BOTTOM = 't';

        private const char RIGHT_ONE = 'o';
        private const char RIGHT_TWO = 'l';
        private const char RIGHT_THREE = 's';

        private const char BOTTOM_PURPLE = 'x';
        private const char BOTTOM_ORANGE = 'c';
        private const char BOTTOM_GREEN = 'v';
        private const char BOTTOM_BLUE = 'b';
        private const char BOTTOM_YELLOW = 'n';
        private const char BOTTOM_RED = 'k';

        private const char TOP_TELEPHONE = 'a';
        private const char TOP_ROLLER = 'z';
        private const char TOP_HAND = 'i';

        public frmMain()
        {
            InitializeComponent();
        }

        private void KeyPressHandle(object sender, KeyPressEventArgs e)
        {
            ComfyKeyboardKeyPressHandle(e);
        }

        private void ComfyKeyboardKeyPressHandle(KeyPressEventArgs e)
        {
            DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
            string MediaFolder = dir.Parent.Parent.Parent.Parent.FullName+@"\media";
            switch (e.KeyChar)
            {
                case LEFT_ONE :
                    /* WAV
                     * string TheSound = mediaFolder+@"\wav\HappyBirthday.wav";
                     * SoundPlayer simpleSound = new SoundPlayer(TheSound);
                     * simpleSound.Play();
                     */

                    // MIDI
                        string TheSound = MediaFolder +@"\midi\HappyBirthday.mid";
                        sequencer1.Stop();
                        sequence1.LoadAsync(TheSound);
                    break;
                case LEFT_TWO :
                    string TheFolder = MediaFolder + @"\images\HappyBirthday";
                    ShowPicturesFromFolder(TheFolder);
                    break;
                case LEFT_THREE :
                    pic.Visible = false; 
                    AxShockwaveFlash axShockwaveFlash= new AxShockwaveFlash();
                    axShockwaveFlash.Location = new System.Drawing.Point(0, 0);
                    this.Controls.Add(axShockwaveFlash);
                    axShockwaveFlash.Movie=MediaFolder+@"\flash\BirthDay_Table_Scene_1.swf";
                    axShockwaveFlash.Size = new System.Drawing.Size(800, 600);
                    axShockwaveFlash.Play();
                    break;
                case LEFT_FOUR :
                    TheSound = MediaFolder + @"\mp3\birthday_back_buddy1_SP.mp3";
                    string sCommand;
                    sCommand = "close \"" + TheSound + "\"" ;
                    mciSendString(sCommand, null, 0, IntPtr.Zero);
                    sCommand = "close MediaFile";
                    mciSendString(sCommand, null, 0, IntPtr.Zero);                    
                    sCommand = "open \"" + TheSound + "\" type mpegvideo alias MediaFile";
                    mciSendString(sCommand, null, 0, IntPtr.Zero);
                    sCommand = "play MediaFile";
                    mciSendString(sCommand, null, 0, IntPtr.Zero);                    
                    break;

                case LEFT_FIVE :
                    break;

                case CENTER_TOP :
                    break;
                case CENTER_LEFT :
                    break;
                case CENTER_CENTER :
                    break;
                case CENTER_RIGHT :
                    break;
                case CENTER_BOTTOM :
                    break;

                case RIGHT_ONE  :
                    break;
                case RIGHT_TWO  :
                    break;
                case RIGHT_THREE :
                    break;

                case BOTTOM_PURPLE :
                    break;
                case BOTTOM_ORANGE:
                    break;
                case BOTTOM_GREEN :
                    break;
                case BOTTOM_BLUE :
                    break;
                case BOTTOM_YELLOW :
                    break;
                case BOTTOM_RED :
                    break;

                case TOP_TELEPHONE:
                    break;
                case TOP_ROLLER :
                    break;
                case TOP_HAND :
                    break;
                default:
                    //do nothing;
                    break;
            }
            e.Handled = true;
        }

        #region PictureDisplay

        private string[] folderFile;
        private int selected;
        private void ShowPicturesFromFolder(string aFolder)
        {
            string[] part1 = null, part2 = null, part3 = null;

            part1 = Directory.GetFiles(aFolder, "*.jpg");
            part2 = Directory.GetFiles(aFolder, "*.jpeg");
            part3 = Directory.GetFiles(aFolder, "*.bmp");

            folderFile = new string[part1.Length + part2.Length + part3.Length];

            Array.Copy(part1, 0, folderFile, 0, part1.Length);
            Array.Copy(part2, 0, folderFile, part1.Length, part2.Length);
            Array.Copy(part3, 0, folderFile, part1.Length + part2.Length, part3.Length);

            selected = 0;

            if (folderFile.Length > 0)
            {
                pic.Visible = true;
                showImage(folderFile[selected]);
                timer1.Start();
            }
            else
            {
                //No pics in folder
            }
        }

        private void showImage(string path)
        {
            Image imgtemp = Image.FromFile(path);
            pic.Width = imgtemp.Width / 2;
            pic.Height = imgtemp.Height / 2;
            pic.Image = imgtemp;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            if (selected == folderFile.Length - 1)
            {
                selected = 0;
                showImage(folderFile[selected]);
            }
            else
            {
                selected = selected + 1;
                showImage(folderFile[selected]);
            }
            timer1.Start();
        }
        
        #endregion

        #region  MIDI Section

        private bool closing;
        private OutputDevice outDevice;
        
        protected override void OnLoad(EventArgs e)
        {
            if (OutputDevice.DeviceCount == 0)
            {
                MessageBox.Show("No MIDI output devices available.", "Error!",
                    MessageBoxButtons.OK, MessageBoxIcon.Stop);

                Close();
            }
            else
            {
                try
                {
                    outDevice = new OutputDevice(0);

                    sequence1.LoadProgressChanged += HandleLoadProgressChanged;
                    sequence1.LoadCompleted += HandleLoadCompleted;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error!",
                        MessageBoxButtons.OK, MessageBoxIcon.Stop);

                    Close();
                }
            }

            base.OnLoad(e);
        }
        protected override void OnClosing(CancelEventArgs e)
        {
            closing = true;

            base.OnClosing(e);
        }

        protected override void OnClosed(EventArgs e)
        {
            sequence1.Dispose();

            if (outDevice != null)
            {
                outDevice.Dispose();
            }

            base.OnClosed(e);
        }

        private void HandleLoadCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                sequencer1.Start();
            }
            else
            {
                MessageBox.Show(e.Error.Message);
            }
        }

        private void HandleLoadProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            
        }

        private void HandleChannelMessagePlayed(object sender, Sanford.Multimedia.Midi.ChannelMessageEventArgs e)
        {
            if (closing)
            {
                return;
            }

            outDevice.Send(e.Message);            
        }

        private void HandleChased(object sender, Sanford.Multimedia.Midi.ChasedEventArgs e)
        {
            foreach (ChannelMessage message in e.Messages)
            {
                outDevice.Send(message);
            }
        }

        private void HandlePlayingCompleted(object sender, EventArgs e)
        {

        }

        private void HandleStopped(object sender, Sanford.Multimedia.Midi.StoppedEventArgs e)
        {
            foreach (ChannelMessage message in e.Messages)
            {
                outDevice.Send(message);
            }
        }

        private void HandleSysExMessagePlayed(object sender, Sanford.Multimedia.Midi.SysExMessageEventArgs e)
        {

        }

        #endregion


    }
}